﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "ApplicationEventHandler.h"
#include "IApplicationEvent.h"
#include "IApplicationEventArgs.h"
#include "IApplication.h"
#include "ExternalAddin.h"
#include "AddinHandle.h"
#include "IDocumentManager.h"
#include "IFileExtensions.h"
#include "CommandParameters.h"
#include "ICommandManager.h"
#include "CommandIds.h"
#include "Command\ModuleUtils.h"
#include "IUiDocumentViewManager.h"
#include "Command\CmdOpenFile.h"
#include "GbmpFileSystem.h"
#include "FilePathManager.h"

#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"

using namespace gcmp;
using namespace Sample;

ApplicationEventHandler::ApplicationEventHandler()
{

}

ApplicationEventHandler::~ApplicationEventHandler()
{
    IApplication* pApp = IApplication::Get();
    if (pApp)
    {
        pApp->GetApplicationEvent()->Delete(this);
    }
}

void ApplicationEventHandler::On(IApplicationEventArgs* pArgs)
{
    if (pArgs->GetType() == ApplicationEventType::PreExecute)
    {
        // 设置应用程序BehaviorConfig文件路径，需在日志模块初始化之前
        const std::wstring behaviorConfigPath(gcmp::FileSystem::GetExeDirPath() + L"gbmp_gcmp_behavior_config.xml");
        gcmp::FileInfo info(behaviorConfigPath);
        DBG_WARN_UNLESS(info.IsFile() && info.Exists(), L"配置文件路径无效!", L"GDMP", L"2023-09-30");
        gcmp::FilePathManager::Get()->SetBehaviorConfigFilePath(behaviorConfigPath);
    }
    else if (pArgs->GetType() == ApplicationEventType::PreClose)
    {

    }
    else if (pArgs->GetType() == ApplicationEventType::Executed)
    {
        IUiDocumentViewManager* pUiDocViewMgr = IUiDocumentViewManager::Get();
        if (pUiDocViewMgr)
        {
            pUiDocViewMgr->RegisterDocumentOpenCallback(CmdOpenFile::PrepareUI);
        }
        std::vector<std::wstring> arguments = IApplication::Get()->GetCommandLineArgs();
        if (arguments.size() > 1)
        {
            std::wstring strFile = arguments[1];
            gcmp::FilePath filePath(strFile);
            if (filePath.GetExtension() == IDocumentManager::Get()->GetFileExtensions()->GetDefaultFileExt())
            {
                gcmp::CommandParameters params;
                params[L"FilePath"] = strFile;
                ICommandManager* pCommandMgr = ICommandManager::Get();
                DBG_WARN_AND_RETURN_VOID_UNLESS(pCommandMgr, L"pCommandMgr不存在", L"GDMP", L"2023-09-30");
                pCommandMgr->SendCommand(ID_CMD_OPEN_FILE, params);
            }
        }

        ExternalAddin::Get()->Setup();
        AddinHandle::Get()->Setup();
        ModuleUtils::LoadDwgModule();
    }
}